package com.hema.auction.activity;

import android.Manifest;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.ActivityCompat;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.hema.auction.R;
import com.hema.auction.base.BaseActivity;
import com.hema.auction.fragment.LoginFragment;
import com.hema.auction.fragment.RegisterFragment;
import com.hema.auction.utils.Constant;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * author by john
 * 登录
 */
public class LoginActivity extends BaseActivity {

    @BindView(R.id.iv_cancel)
    ImageView ivCancel;
    @BindView(R.id.tv_login)
    TextView tvLogin;
    @BindView(R.id.tv_register)
    TextView tvRegister;
    private LoginFragment loginFragment;
    private RegisterFragment registerFragment;
    private FragmentManager fragmentManager;
    private FragmentTransaction transaction;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        ButterKnife.bind(this);
        writeExternalPermission();
        fragmentManager = getSupportFragmentManager();
        tvLogin.performClick();
    }

    private void changeFragment(View v) {
        transaction = fragmentManager.beginTransaction();
        switch (v.getId()) {
            case R.id.tv_login:
                if (loginFragment == null) {
                    loginFragment = new LoginFragment();
                    transaction.add(R.id.fragment, loginFragment);
                } else {
                    transaction.show(loginFragment);
                }
                if (registerFragment != null) transaction.hide(registerFragment);
                break;
            case R.id.tv_register:
                if (registerFragment == null) {
                    registerFragment = new RegisterFragment();
                    transaction.add(R.id.fragment, registerFragment);
                } else {
                    transaction.show(registerFragment);
                }
                if (loginFragment != null) transaction.hide(loginFragment);
                break;
        }
        transaction.commitAllowingStateLoss();

    }

    @OnClick({R.id.iv_cancel, R.id.tv_login, R.id.tv_register})
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_cancel:
                finish();
                break;
            case R.id.tv_login:
                tvLogin.setTextColor(getResources().getColor(R.color.white));
                tvRegister.setTextColor(getResources().getColor(R.color.white_59));
                tvLogin.setEnabled(false);
                tvRegister.setEnabled(true);
                changeFragment(v);
                break;
            case R.id.tv_register:
                tvRegister.setTextColor(getResources().getColor(R.color.white));
                tvLogin.setTextColor(getResources().getColor(R.color.white_59));
                tvLogin.setEnabled(true);
                tvRegister.setEnabled(false);
                changeFragment(v);
                break;
        }

    }

    /**
     * 分享到qq空间
     */
//    private void shareToQzone() {
//        Bundle params = new Bundle();
//        params.putInt(QzoneShare.SHARE_TO_QZONE_KEY_TYPE, QzoneShare.SHARE_TO_QZONE_TYPE_IMAGE_TEXT);
//        params.putString(QzoneShare.SHARE_TO_QQ_TITLE, "标题");//必填
//        params.putString(QzoneShare.SHARE_TO_QQ_SUMMARY, "摘要");//选填
//        params.putString(QzoneShare.SHARE_TO_QQ_TARGET_URL, "http://www.hicsg.com");//必填
//        ArrayList<String> imageUrls = new ArrayList<>();
//        imageUrls.add("http://media-cdn.tripadvisor.com/media/photo-s/01/3e/05/40/the-sandbar-that-links.jpg");
//        params.putStringArrayList(QzoneShare.SHARE_TO_QQ_IMAGE_URL, imageUrls);
//        tencent.shareToQzone(this, params, qqLoginListener);
//    }

    /**
     * 分享给qq好友
     */
//    public void shareToQQ() {
//        final Bundle params = new Bundle();
//        params.putInt(QQShare.SHARE_TO_QQ_KEY_TYPE, QQShare.SHARE_TO_QQ_TYPE_DEFAULT);
//        //分享的标题。注：PARAM_TITLE、PARAM_IMAGE_URL、PARAM_    SUMMARY不能全为空，最少必须有一个是有值的。
//        params.putString(QQShare.SHARE_TO_QQ_TITLE, "title");
//        params.putString(QQShare.SHARE_TO_QQ_SUMMARY, "content");
//        //这条分享消息被好友点击后的跳转URL。
//        params.putString(QQShare.SHARE_TO_QQ_TARGET_URL, "http://www.hicsg.com");
//        //分享的图片URL
//        params.putString(QQShare.SHARE_TO_QQ_IMAGE_URL, "http://www.hicsg.com");
//        //手Q客户端顶部，替换“返回”按钮文字，如果为空，用返回代替
//        params.putString(QQShare.SHARE_TO_QQ_APP_NAME, getString(R.string.app_name));
////        params.putInt(QQShare.SHARE_TO_QQ_EXT_INT, "其他附加功能");
//        tencent.shareToQQ(this, params, qqLoginListener);
//    }
    private void writeExternalPermission() {
        if (ActivityCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE)
                != PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},
                    Constant.REQUEST_CODE_PERMISS_WRITE_EXTERNAL_STORAGE);
        } else {

        }
    }
}
